Test Series - java script

Test Number 12/92

Q: What kind of scoping does JavaScript use?
A. Literal
B. Lexical
C. Segmental
D. Sequential
Solution: Like most modern programming languages, JavaScript uses lexical scoping. This means that functions are executed using the variable scope that was in effect when they were defined, not the variable scope that is in effect when they are invoked.
Q: What must be done in order to implement Lexical Scoping?
A. Get the object
B. Dereference the current scope chain
C. Reference the current scope chain
D. Return the value
Solution: In order to implement lexical scoping, the internal state of a JavaScript function object must include not only the code of the function but also a reference to the current scope chain.
Q: What is closure?
A. Function objects
B. Scope where function’s variables are resolved
C. Both Function objects and Scope where function’s variables are resolved
D. Function return value
Solution: A combination of a function object and a scope (a set of variable bindings) in which the function’s variables are resolved is called a closure.
Q: Which of the following is not an example of closures?
A. Objects
B. Variables
C. Functions
D. Graphics
Solution: In JavaScript, closures are created every time a function is created, at function creation time. Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them.
Q: Which of the following uses a lot of CPU cycles?
A. GUI
B. Statically generated graphics
C. Dynamically generated graphics
D. Generic scoping
Solution: Dynamic graphics for data, means simulating motion or movement using the computer. It may also be thought of as multiple plots linked by time. Hence dynamically generating graphics from real-time data uses a lot of CPU cycles.
Q: What is the fundamental rule of lexical scoping?
A. Functions are declared in the scope
B. Functions are executed using scope chain
C. Functions are declared outside the scope
D. Variables are declared within the function
Solution: The fundamental rule of lexical scoping is that the JavaScript functions are executed using the scope chain that was in effect when they were defined.
Q: What is the opposite approach to the lexical scoping?
A. Literal scoping
B. Static scoping
C. Dynamic scoping
D. Generic scoping
Solution: The opposite approach to the lexical scoping is the dynamic scoping. Dynamic scoping does not care how the code is written, but instead how it executes. Each time a new function is executed, a new scope is pushed onto the stack. This scope is typically stored with the function’s call stack. When a variable is referenced in the function, the scope in each call stack is checked to see if it provides the value.
Q: What is the purpose of the dynamic scoping?
A. Variables can be declared outside the scope
B. Variables must be declared outside the scope
C. Variables cannot be declared outside the scope
D. Variable cannot be declared within the function
Solution: Dynamic scoping creates variables that can be called from outside the block of code in which they are defined. A variable declared in this fashion is sometimes called a public variable.
Q: Which of the algorithmic languages is not lexical scoping standardized in?
A. Ada
B. Pascal
C. Modula2
D. Html
Solution: Lexical scoping is standardized in all algorithmic languages (ALGOL), such as Ada, Pascal, and Modula2. Additionally, it is used in modern functional languages like ML and Haskell.
Q: What will be the function of the following JavaScript code?

var scope = "global scope";
function checkscope() 
{
    var scope = "local scope"; 
    function f() 
    { 
         return scope; 
    }
    return f;
}
A. Returns value null
B. Returns exception
C. Returns the value in scope
D. Shows an error message
Solution: In JavaScript, every running function, code block, and the script as a whole have an associated object known as the Lexical Environment. The above code snippet returns the value in scope.

You Have Score    /10